﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Service;
using WmsApp.Window.Editor;
using WmsApp.WindowControl;
using WmsApp.WmsService;

namespace WmsApp.UI
{
    public class ColumnExtraButtonAttacher
    {
        private static readonly List<ColumnExtraButtonAttacher> ColumnExtraButtonAttachers =
            new List<ColumnExtraButtonAttacher>();
        public static ColumnExtraButtonAttacher AttachToColumn(RepositoryItemGridLookUpEdit repositoryItemGridLookUpEdit, GridView gridView)
        {
            var attacher = new ColumnExtraButtonAttacher(repositoryItemGridLookUpEdit, gridView);
            ColumnExtraButtonAttachers.Add(attacher);
            return attacher;
        }

        private readonly RepositoryItemGridLookUpEdit _gridLookUpEdit = null;
        private readonly GridView _gridView = null;
        public ColumnExtraButtonAttacher(RepositoryItemGridLookUpEdit repositoryItemGridLookUpEdit, GridView gridView)
        {
            _gridLookUpEdit = repositoryItemGridLookUpEdit;
            this._gridView = gridView;
            RegEvents();
        }

        private void RegEvents()
        {
            this._gridLookUpEdit.KeyDown += GridLookUpEditKeyDown;
        }

        private void GridLookUpEditKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                AddGoodsCodeType();
            }
            else if (e.KeyCode == Keys.F3)
            {
                EditGoodsCodeType();
            }
        }

        public void EditGoodsCodeType()
        {
            var obj = this._gridView.GetFocusedRow();
            if (obj != null)
            {
                var instoreBillDtl = (InStoreBillDtl)obj;

                IEntityService<InStoreBill, InStoreBillService> entityService =
                    Service.EntityServiceFactory.GetEntityService<InStoreBill, InStoreBillService>();
                var instoreBill = entityService.GetEntity(instoreBillDtl.BillID);

                if (instoreBill == null)
                {
                    //如果为空，则没有保存到服务器
                }
                else if (instoreBill.BillState == Constant.BillStateAffirm)//已经确认，不能修改。
                {
                    Exceptions.ExceptionHandler.ShowException("该入库单已经确认，不能修改物料代码！");
                    return;
                }

                if (!String.IsNullOrEmpty(instoreBillDtl.NewGoodsCode))
                {
                    var goodsCodeType = StaticData.StaticObjects.GetGoodsCodeTypes().Find(g => g.NewGoodsCode ==
                                                                           instoreBillDtl.NewGoodsCode);
                    if (goodsCodeType != null)
                    {
                        var form = new FormGoodsCodeTypeSmallEditor(goodsCodeType);
                        form.ShowDialog(FormMain.FormMainInstance);
                        if (form.DialogResult == System.Windows.Forms.DialogResult.OK)
                        {
                            var gridView = this._gridView;
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["NewGoodsCode"], form.FGoodsCodeType.NewGoodsCode);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["NewGoodsName"], form.FGoodsCodeType.NewGoodsName);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["OldGoodsCode"], form.FGoodsCodeType.OldGoodsCode);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["StoreTypeCode"], form.FGoodsCodeType.StoreTypeCode);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["StoreTypeName"], form.FGoodsCodeType.StoreTypeName);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["UnitCode"], form.FGoodsCodeType.UnitCode);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["UnitName"], form.FGoodsCodeType.UnitName);
                            gridView.SetRowCellValue(gridView.FocusedRowHandle, gridView.Columns["StoreLocation"], form.FGoodsCodeType.StoreLocation);
                        }
                    }
                }
            }
        }

        public void AddGoodsCodeType()
        {
            var obj = this._gridView.GetFocusedRow();
            if (obj != null)
            {
                var instoreBillDtl = (InStoreBillDtl)obj;
                if (!String.IsNullOrEmpty(instoreBillDtl.NewGoodsCode))
                {
                    Exceptions.ExceptionHandler.ShowException("该入库明细物料代码已经存在！");
                    return;
                }
            }
            var form = new Window.Editor.FormGoodsCodeTypeSmallEditor();
            form.ShowDialog(FormMain.FormMainInstance);
            if (form.DialogResult == DialogResult.OK)
            {
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["NewGoodsCode"], form.FGoodsCodeType.NewGoodsCode);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["NewGoodsName"], form.FGoodsCodeType.NewGoodsName);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["OldGoodsCode"], form.FGoodsCodeType.OldGoodsCode);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["StoreTypeCode"], form.FGoodsCodeType.StoreTypeCode);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["StoreTypeName"], form.FGoodsCodeType.StoreTypeName);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["UnitCode"], form.FGoodsCodeType.UnitCode);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["UnitName"], form.FGoodsCodeType.UnitName);
                _gridView.SetRowCellValue(_gridView.FocusedRowHandle, _gridView.Columns["StoreLocation"], form.FGoodsCodeType.StoreLocation);
            }
        }
    }
}
